package fr.mma.scrumm.resources;

import java.util.logging.Logger;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.gson.Gson;

import fr.mma.scrumm.dao.Dao;
import fr.mma.scrumm.factory.StoryFactory;
import fr.mma.scrumm.model.Story;

@Path("/story")
public class StoryResource {
	Logger log = Logger.getLogger(StoryResource.class.getName());
	
	StoryFactory storyFactory = new StoryFactory();

	Dao<Story> dao = new Dao<Story>(Story.class);
	
	Gson gson= new Gson();
	
	@POST
	@Consumes("application/json")
	@Produces("text/plain")
	public String write(String json){
		Story story = gson.fromJson(json, Story.class);
		dao.save(story);
		return "ok";
	}

	@GET
	@Path("/json")
	@Produces("application/json")
	public String getHello(){
		return gson.toJson(storyFactory.build(dao.getAll()));
	}

	@GET
	@Path("{id}/json")
	@Produces("application/json")
	public String getSpringByIdJSON(@PathParam("id") String id) {
		Story story = dao.getByKey(KeyFactory.stringToKey(id));
		return gson.toJson(storyFactory.build(story));
	}
}
